"ui";
importClass(org.opencv.imgcodecs.Imgcodecs);//导入opencv的包
const tool = require('./tool.js');
const dyfd = require('./dyfd.js');
const wxfd = require('./wxfd.js');
const spyl = require('./spyl.js');
const zbyl = require('./zbyl.js');
const ttyy = require('./ttyy.js');
var wxfdtypes = "综合|全部|服饰|鞋靴箱包|家纺布艺|食品|酒水|生鲜|美妆|珠宝文玩|图书|数码家电|家清日用|家装建材|母婴|玩具乐器|运动户外|汽摩|宠物绿植|本地生活|教育培训"
ui.layout(
    <vertical h="4000">
        <horizontal>
            <text text="搜索词" />
            <input id="searchWord" w="200" text="食品" />
        </horizontal>
        <horizontal>
            <text text="评论词" />
            <input id="pinglun" w="200" text="求中福袋，求中福袋，点我主页有惊喜" />
        </horizontal>
        <horizontal>
            <text text="可以参与的福袋最长等待时间" />
            <input id="maxOpenTime" inputType="number" w="200" text="180" />
        </horizontal>
        <horizontal>
            <text text="立即进入开奖的最短时间(秒)" />
            <input id="minOpenTime" inputType="number" w="200" text="80" />
        </horizontal>
        <horizontal>
            <text text="可以参与的福袋最低价值" />
            <input id="maxMoney" inputType="number" w="200" text="100" />
        </horizontal>
        <horizontal>
            <text text="黑名单直播间" />
            <input id="blackLiveList" inputType='textMultiLine' w="200" h="auto" text="与辉同行,游鹿鹿很认真,东方甄选,辰辰妈,浩哥女装,东方甄选美丽生活,云舍花卉,顽皮小老板手作碱水贝果,湖远行" />
        </horizontal>
        <horizontal>
            <text text="中奖提示声音次数" />
            <input id="winCount" inputType="number" w="200" text="5" />
        </horizontal>
        <horizontal>
            <text text="微信福袋类型" />
            <spinner id="wxfdtype" entries="{{wxfdtypes}}" />
        </horizontal>
        <horizontal>
            <text text="卡密" />
            <input id="keygen" w="200" text="" />
            <button id="saveParam" text="保存" />
        </horizontal>
        <horizontal>
            <text id="jh_result" text="" visibility="gone" />
        </horizontal>
        <horizontal>
            <button id="dyfd" text="抖音福袋" />
            <button id="wxfd" text="微信福袋" />
            <button id="spyl" text="视频引流" />
            <button id="ttyy" text="tt语音" />
        </horizontal>

    </vertical>
);

//抢抖音福袋
ui.dyfd.click(function () {
    threads.start(function () {
        if (!checkCar()) {
            return
        }
        tool.init()
        let params = updateParam()
        dyfd.main(params)
    });
});

//抢微信福袋
ui.wxfd.click(function () {
    threads.start(function () {
        if (!checkCar()) {
            return
        }
        let params = updateParam()
        tool.init()
        wxfd.main(params)
    });
});

//视频引流
ui.spyl.click(function () {
    threads.start(function () {
        if (!checkCar()) {
            return
        }
        tool.init()
        let params = updateParam()
        spyl.main(params)
    });
});

//直播引流
// ui.zbyl.click(function () {
//     threads.start(function () {
//         tool.init() 
//         if(!checkCar()){
//             return
//         }
//         let params = updateParam()
//         zbyl.main(params)
//     });
// });

//tt语音
ui.ttyy.click(function () {
    threads.start(function () {
        if (!checkCar()) {
            return
        }
        let params = updateParam()
        ttyy.main(params)
    });
});

//收集参数
function updateParam() {
    var params = {}
    //搜索词
    params.searchWord = ui.searchWord.text()
    //评论
    params.pinglun = ui.pinglun.text()
    //可以参与的福袋最长等待时间
    params.maxOpenTime = ui.maxOpenTime.text()
    //立即进入开奖的最短时间(秒)
    params.minOpenTime = ui.minOpenTime.text()
    //可以参与的福袋最低价值
    params.maxMoney = ui.maxMoney.text()
    //黑名单直播间
    params.blackLiveList = ui.blackLiveList.text().split(',')
    //中奖提示声音次数
    params.winCount = ui.winCount.text()
    //卡密
    params.keygen = ui.keygen.text()
    //激活状态
    params.jh_result = ui.jh_result.getText().toString();
    //微信福袋类型
    params.wxfdtype =  ui.wxfdtype.getSelectedItem();
   
    return params
}

//读取参数
function readParam() {
    try {
        let p = files.read("/sdcard/yldrb.txt")
        let params = JSON.parse(p)
        if (params.searchWord !== "" && params.searchWord !== undefined && params.searchWord !== null) {
            ui.searchWord.text(params.searchWord)
        }
        if (params.pinglun !== "" && params.pinglun !== undefined && params.pinglun !== null) {
            ui.pinglun.text(params.pinglun)
        }
        if (params.maxOpenTime !== "" && params.maxOpenTime !== undefined && params.maxOpenTime !== null) {
            ui.maxOpenTime.text(params.maxOpenTime)
        }
        if (params.minOpenTime !== "" && params.minOpenTime !== undefined && params.minOpenTime !== null) {
            ui.minOpenTime.text(params.minOpenTime)
        }
        if (params.maxMoney !== "" && params.maxMoney !== undefined && params.maxMoney !== null) {
            ui.maxMoney.text(params.maxMoney)
        }
        if (params.blackLiveList !== "" && params.blackLiveList !== undefined && params.blackLiveList !== null) {
            ui.blackLiveList.text(params.blackLiveList.join(','))
        }
        if (params.winCount !== "" && params.winCount !== undefined && params.winCount !== null) {
            ui.winCount.text(params.winCount)
        }
        if (params.keygen !== "" && params.keygen !== undefined && params.keygen !== null) {
            ui.keygen.text(params.keygen)
        }
        if (params.jh_result !== "" && params.jh_result !== undefined && params.jh_result !== null) {
            ui.jh_result.setText(params.jh_result)
        }
        if (params.wxfdtype !== "" && params.wxfdtype !== undefined && params.wxfdtype !== null) {
            let index = wxfdtypes.split('|').indexOf(params.wxfdtype)
            ui.wxfdtype.setSelection(index, true);
        }
    } catch (e) {
        console.log("读取参数异常:" + e)
    }
}


//更新卡密信息
function updateCar() {
    threads.start(function () {
        try {
            //卡密
            let keygen = ui.keygen.text()
            var url = "http://124.71.21.184:7151/app-api/verify/card/bind";
            // var url = "http://192.168.31.212:48080/app-api/verify/card/bind";
            console.log(url)
            let id;
            if (device.sdkInt < 29) {
                id = device.getIMEI();
            } else {
                id = device.getAndroidId();
            }
            let deviceId = null;
            try {
                let p = files.read("/sdcard/yldrb.txt")
                p = JSON.parse(p)
                if (p.deviceId === undefined || p.deviceId === null || p.deviceId === "") {
                    deviceId = id;
                } else {
                    deviceId = p.deviceId;
                }
            } catch (e) {
                deviceId = id;
            }
            let r = http.postJson(url, {
                "cardNumber": keygen,
                "cardDeviceNumber": deviceId
            });
            let d = JSON.parse(r.body.string())
            console.log(d)
            let msg
            if (d.code === 0) {
                msg = "激活成功"
            } else {
                msg = d.msg
            }
            //更新UI
            ui.run(() => {
                ui.jh_result.setText(msg)
                let params = updateParam()
                params.deviceId = deviceId
                params = JSON.stringify(params)
                console.log(params)
                files.write("/sdcard/yldrb.txt", params);
            });
        } catch (e) {
            console.log(e)
            toastLog("卡密服务异常,请检查网络")
        }
    });
}

//效验卡密信息
function checkCar() {
    let keygen = ui.keygen.text()
    if (keygen === "") {
        toastLog("请输入卡密")
        return false
    }
    let jh = ui.jh_result.getText().toString()
    if (jh === "激活成功") {
        return true
    } else {
        toastLog(jh)
        return false;
    }
}

//保存
ui.saveParam.click(function () {
    updateCar()
    setTimeout(() => {
        toastLog(ui.jh_result.getText().toString())
    }, 1000)
})


//读取参数
readParam()
//更新卡密信息
updateCar()